/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.lnu.ums.treestructure.impl;

import edu.lnu.ums.entities.core.UMSObject;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class TreeNode implements Serializable {
    private UMSObject data;

    private String id;

    private String name;
    
    private TreeNode parent;
    
    private List<TreeNode> children = new ArrayList<TreeNode>();

    public TreeNode(String key, UMSObject obj) {
        this.id = key;
        this.name = obj.getNodeName();
        this.data = obj;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public TreeNode getParent() {
        return parent;
    }

    public void setParent(TreeNode parent) {
        this.parent = parent;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public boolean containIdAmongChildren(String id) {
        for (TreeNode node : children) {
            if (node.getId().equals(id)) {
                return true;
            }
        }

        return false;
    }

    public TreeNode getChildById(String id) {
        if (this.getId().equals(id)) {
            return this;
        }
        if (children == null) {
            return null;
        }

        for (TreeNode child : children) {
            TreeNode temp = child.getChildById(id);
            if (temp != null) {
                return temp;
            }
        }

        return null;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final TreeNode other = (TreeNode) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 47 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    public UMSObject getData() {
        return data;
    }

    public void setData(UMSObject data) {
        this.data = data;
    }


}
